package com.algorithm.tree;

import javax.swing.tree.TreeNode;
import java.util.LinkedList;
import java.util.Optional;
import java.util.Queue;

/**
 * 二叉树遍历
 *
 * @author : fujc-dev@qq.com
 * @created : 2022/9/4 20:12
 */


public class BinaryTree {

    static class TreeNode {
        private char data;
        private TreeNode left;
        private TreeNode right;

        public char getData() {
            return data;
        }

        public void setData(char data) {
            this.data = data;
        }

        public TreeNode getLeft() {
            return left;
        }

        public void setLeft(TreeNode left) {
            this.left = left;
        }

        public TreeNode getRight() {
            return right;
        }

        public void setRight(TreeNode right) {
            this.right = right;
        }

        public TreeNode(char data, TreeNode left, TreeNode right) {
            this.data = data;
            this.left = left;
            this.right = right;
        }
    }

    public void print(TreeNode node) {
        System.out.print(node.getData());
    }

    //前序遍历 根 左 右
    public void pre(TreeNode root) {
        //根输出
        print(root);
        if (root.getLeft() != null) {
            pre(root.getLeft());  // 子树，已它为root节点，分解子问题
        }
        if (root.getRight() != null) {
            pre(root.getRight());
        }
    }

    // 中序遍历 左 根 右
    public void in(TreeNode root) {
        if (root.getLeft() != null) {
            pre(root.getLeft());  // 子树，已它为root节点，分解子问题
        }
        print(root);
        if (root.getRight() != null) {
            pre(root.getRight());
        }
    }

    // 后序比阿尼 左 右 根
    public void post(TreeNode root) {
        if (root.getLeft() != null) {
            pre(root.getLeft());  // 子树，已它为root节点，分解子问题
        }
        if (root.getRight() != null) {
            pre(root.getRight());
        }
        print(root);
    }

    // 层次遍历
    public void level(TreeNode root) {
        if (!Optional.ofNullable(root).isPresent()) {
            System.out.println("q2323");
            return;
        }
        Queue<TreeNode> queues = new LinkedList<>();
    }


    public static void main(String[] args) {
        TreeNode K = new TreeNode('K', null, null);
        TreeNode H = new TreeNode('H', null, null);
        TreeNode G = new TreeNode('G', H, K);
        TreeNode F = new TreeNode('F', G, null);
        TreeNode E = new TreeNode('E', null, F);
        TreeNode D = new TreeNode('D', null, null);
        TreeNode C = new TreeNode('C', null, D);
        TreeNode B = new TreeNode('B', null, C);
        TreeNode A = new TreeNode('A', B, E);

        BinaryTree binaryTree = new BinaryTree();
        System.out.println("前序遍历：");
        binaryTree.pre(A);
        System.out.println("");
        System.out.println("中序遍历：");
        binaryTree.in(A);
        System.out.println("");
        System.out.println("后序遍历：");
        binaryTree.post(A);
        binaryTree.level(null);
    }
}
